package com.example.test.mapper;

import com.example.test.entity.Order;
import com.example.test.entity.Product;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    @Select("SELECT * FROM orders")
    List<Order> findAllOrders();

    @Select("SELECT * FROM orders WHERE id = #{id}")
    Order findOrderById(Long id);

    @Insert("INSERT INTO orders (status, totalAmount, username) VALUES (#{status}, #{totalAmount},#{username})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertOrder(Order order);

    @Update("UPDATE orders SET status = #{status} WHERE id = #{orderId}")
    void updateOrderStatus(@Param("orderId") Long orderId, @Param("status") String status);

    @Insert("INSERT INTO refund_reasons (orderId, reason) VALUES (#{orderId}, #{reason})")
    void insertRefundReason(@Param("orderId") Long orderId, @Param("reason") String reason);

    @Select("SELECT p.*, op.quantity AS orderQuantity FROM products p " +
            "JOIN order_products op ON p.id = op.productId " +
            "WHERE op.orderId = #{orderId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "image", column = "image"),
            @Result(property = "description", column = "description"),
            @Result(property = "quantity", column = "quantity"),
            @Result(property = "price", column = "price"),
            @Result(property = "orderQuantity", column = "orderQuantity")
    })
    List<Product> findProductsByOrderId(Long orderId); // 获取订单产品列表

    @Insert("INSERT INTO order_products (orderId, productId, quantity) VALUES (#{orderId}, #{productId}, #{quantity})")
    void insertOrderProduct(@Param("orderId") Long orderId, @Param("productId") Long productId, @Param("quantity") int quantity);

    @Select("SELECT COUNT(*) FROM reviews WHERE orderId = #{orderId} AND productId = #{productId} AND username = #{username}")
    int reviewExists(@Param("orderId") Long orderId, @Param("productId") Long productId, @Param("username") String username);

    @Insert("INSERT INTO reviews (orderId, productId, review, username) VALUES (#{orderId}, #{productId}, #{review}, #{username})")
    void insertReview(@Param("orderId") Long orderId, @Param("productId") Long productId, @Param("review") String review, @Param("username") String username);

    @Update("UPDATE reviews SET review = #{review} WHERE orderId = #{orderId} AND productId = #{productId} AND username = #{username}")
    void updateReview(@Param("orderId") Long orderId, @Param("productId") Long productId, @Param("review") String review, @Param("username") String username);

    @Select("SELECT review FROM reviews WHERE orderId = #{orderId} AND productId = #{productId} AND username = #{username}")
    String findReviewByOrderProductAndUsername(@Param("orderId") Long orderId, @Param("productId") Long productId, @Param("username") String username);



    @Select("SELECT o.*, rr.reason FROM orders o " +
            "JOIN refund_reasons rr ON o.id = rr.orderId " +
            "WHERE o.status = '申请退款中'")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "totalAmount", column = "totalAmount"),
            @Result(property = "reason", column = "reason")
    })
    List<Order> findOrdersByStatus(String status);

    @Select("SELECT id, orderId, productId, review, username FROM reviews WHERE iflegal = false")
    List<Map<String, Object>> findPendingReviews();

    @Update("UPDATE reviews SET iflegal = #{iflegal} WHERE id = #{reviewId}")
    void updateReviewStatus(@Param("reviewId") Long reviewId, @Param("iflegal") boolean iflegal);

}